<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html><head><title>Programming in Lua : 13</title>
<link rel="stylesheet" type="text/css" href="pil.css">
</head>
<body>
<p class="warning">
<A HREF="contents.html"><IMG TITLE="Programming in Lua (first edition)" SRC="capa.jpg" ALT="" ALIGN="left"></A>This first edition was written for Lua 5.0. While still largely relevant for later versions, there are some differences.<BR>The third edition targets Lua 5.2 and is available at <A HREF="http://www.amazon.com/exec/obidos/ASIN/859037985X/theprogrammil3-20">Amazon</A> and other bookstores.<BR>By buying the book, you also help to <A HREF="../donations.html">support the Lua project</A>.
</p>
<table width="100%" class="nav">
<tr>
<td width="10%" align="left"><a href="12.1.2.html"><img border="0" src="left.png" alt="Previous"></a></td>
<td width="80%" align="center"><a href="contents.html"><font face="Helvetica,Arial,sanserif">
<font color="gray">Programming in </font><font color="blue"> Lua</font>
</font></a></td>
<td width="10%" align="right"><a href="13.1.html"><img border="0" src="right.png" alt="Next"></a></td>
</tr>
<tr>
<td width="10%" align="left"></td>
<td width="80%" align="center"><a href="contents.html#P2">Part II. Tables and Objects</a>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="contents.html#13">Chapter 13. Metatables and Metamethods</a></td>
<td width="10%" align="right"></td></tr>
</table>
<hr/>
<h1>13 &ndash; Metatables and Metamethods</h1>

<p>Usually, tables in Lua have a quite predictable set of operations.
We can add key-value pairs,
we can check the value associated with a key,
we can traverse all key-value pairs,
and that is all.
We cannot add tables,
we cannot compare tables,
and we cannot call a table.

<p>Metatables allow us to change the behavior of a table.
For instance, using metatables,
we can define how Lua computes the expression <code>a+b</code>,
where <code>a</code> and <code>b</code> are tables.
Whenever Lua tries to add two tables,
it checks whether either of them has a metatable
and whether that metatable has an <code>__add</code> field.
If Lua finds this field, it calls the corresponding value
(the so-called <em>metamethod</em>, which should be a function)
to compute the sum.

<p>Each table in Lua may have its own <em>metatable</em>.
(As we will see later,
userdata also can have metatables.)
Lua always create new tables without metatables:
<pre>
    t = {}
    print(getmetatable(t))   --> nil
</pre>
We can use <code>setmetatable</code> to set or change the
metatable of any table:
<pre>
    t1 = {}
    setmetatable(t, t1)
    assert(getmetatable(t) == t1)
</pre>
Any table can be the metatable of any other table;
a group of related tables may share a common metatable
(which describes their common behavior);
a table can be its own metatable
(so that it describes its own individual behavior).
Any configuration is valid.

<hr/>
<table width="100%" class="nav">
<tr valign="top">
<td width="90%" align="left">
<small>
  Copyright &copy; 2003&ndash;2004 Roberto Ierusalimschy.  All rights reserved.
</small>
</td>
<td width="10%" align="right"><a href="13.1.html"><img border="0" src="right.png" alt="Next"></a></td>
</tr>
</table>


</body></html>

